//
//  MoreProductCell.m
//  MedicineBox
//
//  Created by dllo on 15/9/17.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MoreProductCell.h"
#import "UIImageView+WebCache.h"
#import "CustomTap.h"
#import "DKNightVersion.h"
@interface MoreProductCell ()

@end

@implementation MoreProductCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
            
        for (int i = 0; i < 8; i++) {
            
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(20 + (([[UIScreen mainScreen] bounds].size.width - 40) / 6) * (i % 4) + 35 * (i % 4), 10 + (i / 4) * (([[UIScreen mainScreen] bounds].size.width - 40) / 6) + 30 * (i / 4), (([[UIScreen mainScreen] bounds].size.width - 40) / 6), ([[UIScreen mainScreen] bounds].size.width - 40) / 6)];
            
            if ([[UIScreen mainScreen] bounds].size.width > 375) {
                
                imageV.frame = CGRectMake(20 + (([[UIScreen mainScreen] bounds].size.width - 40) / 6) * (i % 4) + 42 * (i % 4), 10 + (i / 4) * (([[UIScreen mainScreen] bounds].size.width - 40) / 6) + 30 * (i / 4), (([[UIScreen mainScreen] bounds].size.width - 40) / 6), ([[UIScreen mainScreen] bounds].size.width - 40) / 6);
                
            } else if ([[UIScreen mainScreen] bounds].size.width == 375) {
            
                  imageV.frame = CGRectMake(20 + (([[UIScreen mainScreen] bounds].size.width - 40) / 6) * (i % 4) + 37 * (i % 4), 10 + (i / 4) * (([[UIScreen mainScreen] bounds].size.width - 40) / 6) + 30 * (i / 4), (([[UIScreen mainScreen] bounds].size.width - 40) / 6), ([[UIScreen mainScreen] bounds].size.width - 40) / 6);
            
            } else if ([[UIScreen mainScreen] bounds].size.width < 375) {
            
                 imageV.frame = CGRectMake(20 + (([[UIScreen mainScreen] bounds].size.width - 40) / 6) * (i % 4) + 34 * (i % 4), 10 + (i / 4) * (([[UIScreen mainScreen] bounds].size.width - 40) / 6) + 30 * (i / 4), (([[UIScreen mainScreen] bounds].size.width - 40) / 6), ([[UIScreen mainScreen] bounds].size.width - 40) / 6);
            
            }
            
            imageV.tag = 1000 + i;
            imageV.userInteractionEnabled = YES;
            [self.contentView addSubview:imageV];
            [imageV release];
            
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 + (([[UIScreen mainScreen] bounds].size.width - 40) / 4) * (i % 4) + 10 * (i % 4), imageV.frame.size.height + imageV.frame.origin.y , (([[UIScreen mainScreen] bounds].size.width - 30) / 4), 20)];
            titleLabel.nightTextColor = [UIColor whiteColor];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:10];
            [self.contentView addSubview:titleLabel];
            titleLabel.tag = 2000 + i;
           
            [titleLabel release];
            
            
        }
        
        
    }
        
    return self;
}

- (CGFloat)getHeight:(NSString *)context
{

    CGSize size = CGSizeMake(([[UIScreen mainScreen] bounds].size.width - 30) / 4, 100);
  CGRect rect = [context boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10],NSFontAttributeName, nil] context:nil];
    return rect.size.height;

}


- (void)getArray:(NSArray *)array
{

    for (int i = 0; i < array.count; i++) {
        
        UIImageView *imageV = (UIImageView *)[self.contentView viewWithTag:1000 + i];
        [imageV sd_setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:i] objectForKey:@"pic"]] placeholderImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"8276cae9dfaee118f8f0b67f106f2f8d" ofType:@"jpg"]]];
        NSDictionary *dic = [array objectAtIndex:i];
    
        UILabel *titleLabel = (UILabel *)[self.contentView viewWithTag:2000 + i];
       CGFloat height = [self getHeight:[dic objectForKey:@"title"]];
        titleLabel.frame = CGRectMake(5 + (([[UIScreen mainScreen] bounds].size.width - 40) / 4) * (i % 4) + 10 * (i % 4), imageV.frame.size.height + imageV.frame.origin.y , (([[UIScreen mainScreen] bounds].size.width - 30) / 4), height);
        
        titleLabel.numberOfLines = 0;
        titleLabel.text = [dic objectForKey:@"title"];
        
        CustomTap *tap = [[CustomTap alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.dic = dic;
        [imageV addGestureRecognizer:tap];
        [tap release];
    }
    
    
}

- (void)tapAction:(CustomTap *)tap
{

    [(NSObject *)self.target performSelector:self.action withObject:tap.dic];

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
